<template>
  <div class="head">
    <div class="nav content">
      <div class="wrap clearfix">
        <ul class="fl clearfix">
          <li class="home" @click="tohome" v-if="isshow">
            <i class="iconfont icon-shouye"></i> <span>开心购首页</span>
          </li>
          <li class="area">
            <i class="iconfont icon-zuobiao1"></i> <span>四川</span>
          </li>
        </ul>
        <ul class="fr head-fl">
          <li class="fore2" v-if="!show">
            <span class="enter" @click="login"> 你好，请登录 </span>
            &nbsp;&nbsp;
            <span class="login" @click="register"> 免费注册 </span>
          </li>
          <li>
            <div class="user" v-if="show">
              <span>个人用户：</span
              ><img class="headimg" :src="img" alt="" srcset="" />
              <span class="fore1 username" @click="hreforder()">
                {{ name }}</span
              >
              <span class="fore1" @click="quit()">退出</span>
            </div>
          </li>
          <li class="spacer"></li>
          <li class="fore1" @click="toorder">我的订单</li>
          <li class="spacer"></li>
          <li class="fore1" @click="toshopping">我的购物</li>
          <li class="spacer"></li>
          <li class="fore1">购物会员</li>
          <li class="spacer"></li>
          <li class="fore1">企业采购</li>
          <li class="spacer"></li>
          <li class="fore1">客户服务</li>
          <li class="spacer"></li>
          <li class="fore1">网站导航</li>
          <li class="spacer"></li>
          <li class="fore1">手机购物</li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
// import { mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
      isshow: false,
      show: false,
      name: "",
      img: "",
    };
  },
  methods: {
    quit() {
      sessionStorage.setItem("token", 0);
      this.show = false;
      this.$router.push({
        path: "/",
      });
      location.reload();
    },
    hreforder() {
      this.$router.push({
        path: "/userinfo",
      });
    },
    // 注册
    register() {
      this.$router.push("./register");
    },
    // 登录
    login() {
      this.$router.push("./login");
    },
    // 订单页面
    toorder() {
        this.$router.push("./order");
    },
    toshopping() {
        this.$router.push("./shoppingcar");
    },
    tohome() {
      this.$router.push("/");
    },
  },

  mounted() {
    var href = window.location.href;

    if (href == "http://localhost:8080/#/") {
      var i = document.querySelector(".icon-zuobiao1");
      i.style.color = "#f10215";
      this.isshow = false;
    } else {
      this.isshow = true;
    }
    if (sessionStorage.getItem("token") == 1) {
      this.show = true;
      this.$axios
        .post("/user/findByUserId", {
          id: sessionStorage.getItem("userId"),
        })
        .then((res) => {
          this.name = res.data.data.username;
          this.img = res.data.data.headPortrait;
        });
    } else {
      this.show = false;
    }
  },
};
</script>
<style scoped>
.nav {
  background-color: #e3e4e5;
}
.content {
  width: 100%;
}
.home {
  float: left;
  position: relative;
  height: 30px;
  display: inline-block;
  width: 80px;
  cursor: pointer;
}
.area {
  margin-left: 10px;
  float: left;
  position: relative;
  width: 40px;
  height: 30px;
  cursor: pointer;
}
.home:hover span,
.area:hover span {
  color: #f30213;
}
.icon-shouye {
  position: absolute;
  font-size: 14px;
  color: #5c5452;
}
.home span {
  position: absolute;
  left: 20px;
  top: 2px;
}
.icon-zuobiao {
  position: absolute;
  color: #5c5452;
}
.area span {
  position: absolute;
  left: 20px;
  display: inline-block;
  width: 40px;
  top: 2px;
}
.fl {
  float: left;
}
.fr {
  float: right;
}
.head-fl li {
  float: left;
}
.nav {
  height: 30px;
  line-height: 30px;
  color: #999;
  font-size: 12px;
}
.fore1,
.fore2 {
  padding-left: 7px;
  padding-right: 7px;
}
.nav ul > .spacer {
  overflow: hidden;
  margin: 11px 5px 0;
  width: 1px;
  height: 10px;
  background-color: #ccc;
}
.nav li > .login {
  color: #f10215;
}
.fore1:hover,
.login:hover,
.enter:hover {
  color: #f10215;
  cursor: pointer;
}
.clearfix::after {
  content: "";
  display: block;
  clear: both;
  visibility: hidden;
}
.headimg {
  padding-top: 5px;
  width: 15px;
  height: 15px;
}
</style>